package com.atguigu.tingshu.order.service.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderDerate;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.factory.GuiguItemType;
import com.atguigu.tingshu.order.mapper.OrderDerateMapper;
import com.atguigu.tingshu.order.mapper.OrderDetailMapper;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author GQS @Date 2024/11/12/23:41 @Description
 */
@Slf4j
@Service(value = "orderInfoServiceImpl")
@SuppressWarnings({"unchecked", "rawtypes"})
@GuiguItemType(itemType = SystemConstant.ORDER_ITEM_TYPE_ALL)
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

  @Autowired
  private OrderInfoMapper orderInfoMapper;

  @Autowired
  private OrderDetailMapper orderDetailMapper;

  @Autowired
  private OrderDerateMapper orderDerateMapper;
  /**
   * 分页查询用户的订单数据
   * @param page
   * @param size
   * @return
   */
  @Override
  public Object selectUserOrderPage(Integer page, Integer size) {
    return orderInfoMapper.selectPage(new Page<>(page, size),
                                      new LambdaQueryWrapper<OrderInfo>()
                                              .eq(OrderInfo::getUserId, AuthContextHolder.getUserId())
                                              .eq(OrderInfo::getOrderStatus, "0902"));
  }

  /**
   * 查询订单的详细信息
   * @param orderNo
   * @return
   */
  @Override
  public OrderInfo getOrderInfo(String orderNo) {
    // 查询订单信息
    OrderInfo orderInfo = orderInfoMapper.selectOne(
            new LambdaQueryWrapper<OrderInfo>()
                    .eq(OrderInfo::getOrderNo, orderNo)
                    .eq(OrderInfo::getOrderStatus, "0902")
                    .last("limit 1"));
    if (orderInfo == null){
      return null;
    }
    // 查询订单详细信息列表
    List<OrderDetail> orderDetailList =
            orderDetailMapper.selectList(
                    new LambdaQueryWrapper<OrderDetail>()
                            .eq(OrderDetail::getOrderId, orderInfo.getId()));


    // 查询订单折扣信息列表
    List<OrderDerate> orderDerateList =
            orderDerateMapper.selectList(
                    new LambdaQueryWrapper<OrderDerate>()
                            .eq(OrderDerate::getOrderId, orderInfo.getId()));
    // 封装返回结果
    orderInfo.setOrderDetailList(orderDetailList);
    orderInfo.setOrderDerateList(orderDerateList);
    // 返回
    return orderInfo;
  }

  @Override
  public Object trade(TradeVo tradeVo) {
    return null;
  }

  @Override
  public Object submitOrder(OrderInfoVo orderInfoVo) {
    return null;
  }

}
